package com.demo.spring.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

@Aspect
@Component
//强制使用cglib代理
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class AspectJTest {
	
	// 切面Advisor组合了PointCut和Advice，一个Aspect切面会对应多个Advisor

	// @Pointcut定义切点（PointCut）
	@Pointcut("execution(* *.say(..))")
	public void test(){}

	// @Before、@After、@Around都是通知（Advice）
	@Before("test()")
	public void before(){
		System.out.println("before test..");
	}

	@After("test()")
	public void after(){
		System.out.println("after test..");
	}

	@Around("test()")
	public Object around(ProceedingJoinPoint p){
		System.out.println("before1");
		Object o = null;
		try {
			o = p.proceed();
		} catch (Throwable e) {
			e.printStackTrace();
		}
		System.out.println("after1");
		return o;
	}

}
